package licode.unisop.provider.utils;

import licode.unisop.provider.conf.SiAssetType;
import licode.unisop.provider.conf.SiClassifyType;
import licode.unisop.provider.vo.SiAsset;
import licode.unisop.provider.vo.SiAssetBO;
import licode.unisop.provider.vo.SiAssetTKey;

import java.util.List;

/**
 * 资源计算工具类
 *
 * @author licode
 */
public class SiAssetUtil {
    public static void sortAssetBo(List<SiAssetBO> assets) {
        if (null != assets) {
            assets.sort((o1, o2) -> {
                if (o1.getLevel() != null && o2.getLevel() != null) {
                    return o1.getLevel() - o2.getLevel();
                } else {
                    return 0;
                }
            });

            assets.forEach(item -> {
                if (null != item.getChildren() && !item.getChildren().isEmpty()) {
                    sortAssetBo(item.getChildren());
                }
            });
        }
    }

    public static void sortAsset(List<SiAsset> assets) {
        if (null != assets) {
            assets.sort((o1, o2) -> {
                if (o1.getLevel() != null && o2.getLevel() != null) {
                    return o1.getLevel() - o2.getLevel();
                } else {
                    return 0;
                }
            });

            assets.forEach(item -> {
                if (null != item.getChildren() && !item.getChildren().isEmpty()) {
                    sortAsset(item.getChildren());
                }
            });
        }
    }

    public static void sortAssetKeys(List<SiAssetTKey> assets) {
        if (null != assets) {
            assets.sort((o1, o2) -> {
                if (o1.getLevel() != null && o2.getLevel() != null) {
                    return o1.getLevel() - o2.getLevel();
                } else {
                    return 0;
                }
            });
            assets.forEach(item -> {
                if (null != item.getChildren() && !item.getChildren().isEmpty()) {
                    sortAssetKeys(item.getChildren());
                }
            });
        }
    }

    public static boolean isTree(SiAsset asset) {
        return SiClassifyType.ART_TREE.equals(asset.getClassify()) ||
                SiClassifyType.ART_GROUP_TREE.equals(asset.getClassify());
    }

    public static boolean isTree(SiAssetBO asset) {
        return SiClassifyType.ART_TREE.equals(asset.getClassify()) ||
                SiClassifyType.ART_GROUP_TREE.equals(asset.getClassify());
    }

    public static boolean isHold(SiAsset asset) {
        return SiAssetUtil.isHold(asset.getType());
    }

    public static boolean isHold(SiAssetBO asset) {
        return SiAssetUtil.isHold(asset.getType());
    }

    public static boolean isHold(String assetType) {
        return SiAssetType.AT_ROLE.equals(assetType) ||
                SiAssetType.AT_CLIENT.equals(assetType) ||
                SiAssetType.AT_GROUP.equals(assetType);
    }
}
